package com.zp.crm.Controller.MenuController;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.zp.crm.entity.Menu.MenuEntity;
import com.zp.crm.service.MenuService.MenuService;

@Controller
@RequestMapping("/menu")
public class MenuController {
	
	@Resource
	public MenuService menuService;
	
	Gson gson=new Gson();
	@RequestMapping(value="/loginMenu",method=RequestMethod.POST)
	public void LoginMenuList(HttpServletRequest request,HttpServletResponse response,Model model){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		String u_id=request.getParameter("user_id");
		String html=request.getParameter("html");
		Map<String, Object> map=menuService.selectBykeyUserMenuList(u_id,html);
		try {
			PrintWriter out =response.getWriter();
			String text=(String)map.get("message");
			out.println(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/menuZDCreate",method=RequestMethod.POST)
	public void menuZDCreate(HttpServletRequest request,HttpServletResponse response,Model model){
		response.setContentType("text/html;charset=utf-8");
		String roidname=request.getParameter("name");
		System.out.println(roidname);
		String a="";
		Map<String, Object> map=menuService.menuZDCreate(roidname);
		
		if((boolean) map.get("success")){
			a=(String)map.get("message");
		}
		try {
			PrintWriter out = response.getWriter();
			out.println(a);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public void Insert(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String name=request.getParameter("name");
		String u=request.getParameter("u");
		String un=request.getParameter("un");
		String pidstr=request.getParameter("pid");
		String orderstr=request.getParameter("order");
		int pid=0,order=5;
		if(pidstr != null){
			pid=Integer.parseInt(pidstr);
		}
		if(orderstr != null){
			order=Integer.parseInt(orderstr);
		}
		MenuEntity entity=new MenuEntity(pid, name, u, order, new Date(), un);
		
		Map<String, Object> map=menuService.insert(entity);
		
		try {
			PrintWriter out = response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void Delete(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		Map<String, Object> map=menuService.delete(id);
		
		try {
			PrintWriter out = response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public void Edit(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String name=request.getParameter("name");
		String u=request.getParameter("u");
		String un=request.getParameter("un");
		String idstr=request.getParameter("id");
		String orderstr=request.getParameter("order");
		int id=0,order=5;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		if(orderstr != null){
			order=Integer.parseInt(orderstr);
		}
		MenuEntity entity=new MenuEntity(id, name, u, order, new Date(), un);
		Map<String, Object> map=menuService.update(entity);
		try {
			PrintWriter out = response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/selectById",method=RequestMethod.POST)
	public void selectById(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		Map<String, Object> map=menuService.selectById(id);
		try {
			PrintWriter out = response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
